<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>修改角色</title>
	<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	<script src="../../../component/layui/layui.js"></script>
	<script src="../../../component/pear/pear.js"></script>
</head>

<body>
	<form class="layui-form" action="" lay-filter="edit">
		<div class="mainBox">
			<div class="main-container">
				<div class="main-container">
					<div class="layui-form-item">
						<label class="layui-form-label">角色名称</label>
						<div class="layui-input-block">
							<input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入角色名称"
								class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">角色排序</label>
						<div class="layui-input-block">
							<input type="number" name="sort" lay-verify="sort" autocomplete="off" placeholder="请输入角色排序"
								class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">是否有效</label>
						<div class="layui-input-block">
							<input type="checkbox" name="isActive" lay-skin="switch" lay-text="启用|禁用"
								lay-filter="isActive">
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="bottom">
			<div class="button-container">
				<button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="save">
					<i class="layui-icon layui-icon-ok"></i>
					提交
				</button>
				<button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
					<i class="layui-icon layui-icon-refresh"></i>
					重置
				</button>
			</div>
		</div>
	</form>
</body>

</html>

<script>
	layui.use(['form', 'jquery', 'context', 'permission', 'popup', 'common', 'button'], function () {
		let form = layui.form;
		let $ = layui.jquery;
		let context = layui.context;
		let popup = layui.popup;
		let common = layui.common;
		let button = layui.button;

		let basePath = context.get("base-path");
		let currentId = common.getQueryString("id");

		$.ajax({
			url: basePath + '/api/system/role/getinfo',
			data: JSON.stringify({ Id: currentId }),
			success: function (result) {
				form.val("edit", {
					name: result.data.name,
					sort: result.data.sort,
					isActive: result.data.isActive,
				});
			}
		})

		form.on('submit(save)', function (data) {
			var btn = button.load({ elem: 'button[lay-filter=save]' });
			data.field.id = currentId
			data.field.isActive = data.field.isActive == "on" ? true : false;
			$.ajax({
				url: basePath + '/api/system/role/edit',
				data: JSON.stringify(data.field),
				success: function (result) {
					btn.stop(function () {
						if (result.succeeded) {
							popup.success("修改成功", function () {
								parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
								parent.layui.table.reload("table");
							});
						} else {
							popup.failure(JSON.stringify(result.errors));
						}
					})
				}
			})
			return false;
		});
	})
</script>